package com.example.app08_listview;

import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

/**
 * MainActivity
 * @author lijiguo
 */
public class MainActivity_ArrayAdapter extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        Log.i(TAG, "onCreate()");

        ListView lvMain = findViewById(R.id.lv_main);
        // 准备集合数据
        String[] data = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
        "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
        // 准备ArrayAdapter对象
        // context 上下文对象
        // resources 布局资源文件，布局文件跟节点只能是TextView，否则报错
        //      java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
        //      Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
        // objects 数据
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, R.layout.item_array_adapter, data);
        // 设置adapter
        lvMain.setAdapter(arrayAdapter);
    }
}